package com.ophiux.common.tools.util;

import lombok.experimental.Accessors;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;

@Accessors(chain = true)
public class NumberUtil {
    public static String amountFen2Yuan(Integer fen){
        if (fen == null) return null;
        BigDecimal amount = new BigDecimal(fen);
        BigDecimal amountUnit = new BigDecimal(100);
        BigDecimal amountDisplay = amount.divide(amountUnit,2,BigDecimal.ROUND_HALF_UP);
        return String.format("%.2f",amountDisplay.floatValue());
    }
    public static Integer amountYuan2fen(BigDecimal src){
        if (src == null) return null;
        BigDecimal amount = src.setScale(2, RoundingMode.HALF_UP);
        BigDecimal amountUnit = new BigDecimal(100);
        return amount.multiply(amountUnit).intValue();
    }
    public static Integer calculateAmountFee(Integer amount, BigDecimal rate){
        if (amount == null || rate == null)
            return null;
        BigDecimal amountReal = new BigDecimal(amount);
        BigDecimal rateUnit = new BigDecimal(100);
        BigDecimal feeRateReal = rate.divide(rateUnit, 4, RoundingMode.HALF_UP);
        BigDecimal fee = amountReal.multiply(feeRateReal);
        return fee.intValue();
    }
    public static String twoDecimals(BigDecimal src){
        if (src == null) return null;
        BigDecimal amount = src.setScale(2, RoundingMode.HALF_UP);
        return String.format("%.2f",amount.floatValue());
    }
    
    /**
     * 10进制转16进制
     *
     * @param param
     * @return
     */
    public static String parse16(String param) {
        String result = new BigInteger(param, 10).toString(16);
        return result;
    }

    /**
     * 16进制转10进制
     *
     * @param param		16进制数字  01b71a14cc5c58100000
     * @return			8100000000000000000000
     */
    public static String parse10(String param) {
        String result = new BigInteger(param, 16).toString(10);
        return result;
    }
    
    /**
     * 最小单位转为基本单位
     * @param number	8100000000000000000000	单位：attoCoin
     * @return			8100	单位：xfsc
     */
    public static String parseBasicUit(String number) {
    	if("0".equals(number)) return "0";
    	return new BigDecimal(number).divide(new BigDecimal("10").pow(18),18,RoundingMode.DOWN).stripTrailingZeros().toPlainString();
    }
    
    /**
     * 将最小单位转为次单位
     * @param number	150			单位：attoCoin
     * @return			150/10^9 	单位：nanoCoin
     */
    public static String parseNanoCoin(String number) {
    	if("0".equals(number)) return "0";
    	return new BigDecimal(number).divide(new BigDecimal("10").pow(9),9,RoundingMode.DOWN).stripTrailingZeros().toPlainString();
    }
    

    /**
     * 将最小单位转为次单位
     * @param number	150			单位：attoCoin
     * @return			150/10^9 	单位：nanoCoin
     */
    public static String parseAttoCoin(String number) {
    	if("0".equals(number)) return "0";
    	return new BigDecimal(number).multiply(new BigDecimal("10").pow(9)).stripTrailingZeros().toPlainString();
    }
}
